package interfaces;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import services.User;
import services.UserServicesProxy;
/**
 * @author Savva
 *
 */
public class AuthDialog extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTextField nameField = new JTextField(10);
	private JPasswordField passwordField = new JPasswordField(10);
	private JButton okButton = new JButton("Ок");
	private JButton cancelButton = new JButton("Отмена");
	private JButton registration = new JButton("Регистрация");
	private JDialog dialog;
	private boolean selected;
	private User result;
	
	public AuthDialog(JDialog dialog) {
		super();
		this.dialog = dialog;
		initComponents();
		initListeners();
	}
	
	
	private void initListeners() {
		okButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				if(!selected) {
					UserServicesProxy proxy = new UserServicesProxy();
					try {
						result = proxy.authorisation(nameField.getText(),
								new String(passwordField.getPassword()));
					} catch (RemoteException e) {
						e.printStackTrace();
					}
					if(result == null) {
						JOptionPane.showMessageDialog(null, 
								"Не верно введено логин и/или пароль");
						return;
					} 
				} else {
					throw new NullPointerException();
				}
				dialog.dispose();
			}
		});
		cancelButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				result = null;
				dialog.dispose();
			}
		});
		registration.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				RegistrationDialog.showDialog();
			}
		});
	}


	private void initComponents() {
		JPanel panel = new JPanel(new GridBagLayout());
		panel.add(new JLabel("Логин"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		panel.add(nameField, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		nameField.setText("savva");
		panel.add(new JLabel("Пароль"), new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		panel.add(passwordField, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		passwordField.setText("12345678");
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		JPanel registrPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		registrPanel.add(registration);
		JCheckBox checkBox = new JCheckBox("Без авторизации");
		checkBox.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange() == ItemEvent.SELECTED) {
					selected = true;
				} else {
					selected = false;
				}
			}
		});
		registrPanel.add(checkBox);
		buttonPanel.add(registrPanel);
		panel.add(buttonPanel, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		add(panel);
	}



	public User getResult() {
		return result;
	}

	public static User showDialog() {
		JDialog dialog = new JDialog();
		AuthDialog authDialog = new AuthDialog(dialog);
		dialog.add(authDialog);
		dialog.setTitle("Авторизация");
		dialog.setModal(true);
		dialog.setLocationRelativeTo(null);
		dialog.pack();
		dialog.setVisible(true);
		return authDialog.getResult();
	}

}
